package com.background.service;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.background.dao.IWebLinkDao;
import com.background.dao.network.ExtractElements;
import com.background.dao.network.Rule;
import com.background.dao.network.RuleException;
import com.background.model.AdWebsites;

/**
 * Created by helloworld on 17-3-16.
 */
@Service
public class WebLinkService implements IWebLinkService{

	@Autowired
    IWebLinkDao webLinkDao;
	
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    //从远程获取JS链接，并保存到redis缓存
    public void getWebLinkFromRemote(){
    	
    	List<AdWebsites> websitesList = webLinkDao.findAllAdWebSites();
    	for( AdWebsites site : websitesList ){
    		System.out.println("mysql中记录："+ site.toString());
    		Rule rule = new Rule();
    		rule.setUrl(site.getAddr())
    			.setMethod(Rule.Method.GET)
    			.setSelector("script");
    		String siteName=site.getName();
   
    		try {
    			//获取符合的元素
    			Elements elements = ExtractElements.extract(rule);
    			System.out.println("------------爬取站点："+site.getAddr());
    			for (Element element : elements) {
    				//redis缓存每个爬取目标站点的JS链接
    				if(element.attr("src").equals("")) {
    					continue;
    				}
    				SetOperations<String,String> kops = stringRedisTemplate.opsForSet();  				
    				kops.add("EMS_AD_S_"+siteName+"_JS",element.attr("src"));
    			}
    			stringRedisTemplate.boundValueOps("EMS_AD_S_"+siteName+"_JS").expire(120, TimeUnit.MINUTES);//避免不再爬取的站点信息长期残留 在redis中
    		}catch (RuleException e ){
    			//LOG
                e.printStackTrace();
            }catch (IOException e) {
               //LOG
            	e.printStackTrace();
            }
    		
    	}
    }
    
    //读取redis缓存中所有JS链接
    public Set<String> getWebLinkFromCache() {
    	Set<String> set=new HashSet<String>();
    	List<AdWebsites> websitesList = webLinkDao.findAllAdWebSites();
    	for( AdWebsites site : websitesList ){
    		Rule rule = new Rule();
    		rule.setUrl(site.getAddr())
    			.setMethod(Rule.Method.GET)
    			.setSelector("script");
    		String siteName=site.getName();
    		SetOperations<String,String> kops = stringRedisTemplate.opsForSet();  
    		Set<String> res=kops.members("EMS_AD_S_"+siteName+"_JS");
    		set.addAll(res);
    		
    		
    	}
    	return set;
//    	int i=0;
//    	for(String ele: set) {
//    		System.out.println(i+":-----"+ ele);
//    	}
//    	for( Object ele: set.toArray()) {
//    		System.out.println(i+":+++++"+ ele.toString());
//    	}
//    	return set.toArray();
    }
    
    
    public void updateWebInfo(){
        //获取此网站有用的 link 信息
        //ExtractElements

        //存到 redis
        //redis.updateWebSite()
    }
}
